#!/bin/bash

# 创建必要的目录并设置权限
mkdir -p /mnt/user/appdata/hosts
mkdir -p /mnt/downloads
mkdir -p /mnt/nastool/mv
mkdir -p /mnt/nastool/tv
mkdir -p /mnt/nastool/cartoon
mkdir -p /mnt/failed
chmod -R 777 /mnt/nastool
chmod -R 777 /mnt/failed
chmod -R 777 /mnt/downloads
chmod -R 777 /mnt/user/appdata/chronos

# 下载并解压缩 appdata 文件
sudo wget -O /tmp/appdata.tar.gz https://gitee.com/sonata1/code-snippet/releases/download/v1.0/appdata.tar.gz
sudo tar -xzvf /tmp/appdata.tar.gz -C /mnt/user/appdata
sudo rm /tmp/appdata.tar.gz

# 创建 Docker 网络

docker network create --driver bridge --subnet 192.168.66.0/24 --gateway 192.168.66.1 emby

# 启动 emby_alist 容器
docker run -d --restart=always --network emby --ip 192.168.66.2 \
    -v /mnt/user/appdata/alist:/opt/alist/data \
    -v /mnt/downloads:/mnt/downloads \
    --name="emby_alist" xhofe/alist:latest

# 启动 emby_rclone 容器
docker run -d --restart=always --network emby --name=emby_rclone --ip 192.168.66.21 \
    --volume /mnt/user/appdata/rclone:/config/rclone \
    --volume /mnt/alist:/mnt/alist:shared \
    --volume /mnt/alistro:/mnt/alistro:shared \
    --device /dev/fuse --cap-add SYS_ADMIN --security-opt apparmor:unconfined \
    --entrypoint /bin/sh rclone/rclone -c "/config/rclone/script.sh"

# 启动 emby_chronos 容器
docker run -itd -p 9800:9800 -e TZ=Asia/Shanghai --name="emby_chronos" --network emby --ip 192.168.66.10 --restart=always \
    -v /mnt/nastool:/mnt/nastool \
    -v /mnt/user/appdata/hosts/hosts:/mnt/user/appdata/hosts/hosts \
    -v /mnt/user/appdata/chronos:/chronos \
    -v /mnt/alist:/mnt/alist \
    -v /mnt/downloads:/mnt/downloads \
    -e PIP_INDEX_URL=https://pypi.tuna.tsinghua.edu.cn/simple simsemand/chronos

# 启动 emby_nginx 容器
docker run -itd -p 8097:8080 --name emby_nginx --restart=always --network emby --ip 192.168.66.8 \
    -v /mnt/user/appdata/openresty/conf.d/:/etc/nginx/conf.d/ \
    -v /mnt/user/appdata/openresty/nginx/conf/nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf \
    -v /tmp/dockernginx/cache:/tmp/dockernginx/cache \
    -v /tmp/dockernginx/tmp:/tmp/dockernginx/tmp \
    -v /mnt/user/appdata/openresty/log:/var/log/nginx \
    openresty/openresty

# 启动 emby_redis 容器
docker run --name emby_redis -d --network emby --ip 192.168.66.11 --restart=always --cap-add=SYS_PTRACE --security-opt seccomp=unconfined redis --requirepass "bilibili"

# 启动 emby-qbittorrent 容器
docker run -d --name emby-qbittorrent -e WEBUIPORT=8080 --restart=unless-stopped --network emby --ip 192.168.66.6 \
    -e PUID=1026 \
    -e PGID=100 \
    -e TZ=Asia/Shanghai \
    -p 6881:6881 \
    -p 6881:6881/udp \
    -v /mnt/user/appdata/qbittorrent/config:/config \
    -v /mnt/downloads:/mnt/downloads superng6/qbittorrent:latest

# 启动 emby-nastools 容器
docker run -itd --name emby-nastools -p 8099:3000 -e TZ=Asia/Shanghai --network emby --ip 192.168.66.3 \
    -v /mnt/user/appdata/nas-tools/config:/config \
    -v /mnt/user/appdata/hosts/hosts:/etc/hosts \
    -v /mnt/alist:/mnt/alist \
    -v /mnt/failed:/mnt/failed \
    -v /mnt/nastool:/mnt/nastool \
    -v /mnt/downloads:/mnt/downloads \
    -e NASTOOL_AUTO_UPDATE=false anypush/nas-tools:latest

# 启动 chinesesubfinder 容器
docker run -d --network emby --ip 192.168.66.7 -p 9035:19035 \
    -v /mnt/user/appdata/ChineseSubFinder/config:/config \
    -v /mnt/user/appdata/hosts/hosts:/etc/hosts \
    -v /mnt/nastool:/mnt/nastool \
    -v /mnt/downloads:/mnt/downloads \
    -v /mnt/alistro:/mnt/alist \
    --restart=unless-stopped \
    -e PERMS=false \
    -e TZ=Asia/Shanghai \
    --name chinesesubfinder \
    --log-driver "json-file" \
    --log-opt "max-size=100m" \
    allanpk716/chinesesubfinder
docker restart emby-nastools
docker restart emby_chronos
docker restart chinesesubfinder